# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Celeste <cielesti@protonmail.com>
pkgname=merlin
pkgver=4.14
_ocamlver=4.14
_pkgver="$pkgver-${_ocamlver//./}"
pkgrel=0
pkgdesc="Context sensitive completion for OCaml"
url="https://ocaml.github.io/merlin/"
arch="all !riscv64 !loongarch64" # ocaml
license="MIT"
depends_dev="$pkgname=$pkgver-r$pkgrel"
makedepends="
	dune
	ocaml
	ocaml-csexp-dev
	ocaml-findlib-dev
	ocaml-menhir-dev
	ocaml-yojson-dev
	"
checkdepends="cmd:menhir jq ocaml-ppxlib-dev"
options="!check" # requires annotation and source files that we delete in ocaml
subpackages="
	$pkgname-dev
	$pkgname-emacs::noarch
	$pkgname-vim::noarch
	"
source="$pkgname-$_pkgver.tar.bz2::https://github.com/ocaml/merlin/releases/download/v$_pkgver/merlin-$_pkgver.tbz"
builddir="$srcdir/$pkgname-$_pkgver"

# 32-bit archs
case "$CARCH" in
	arm*|x86) options="$options textrels" ;;
esac

build() {
	dune build -p merlin,merlin-lib,dot-merlin-reader \
		@install --no-buffer --verbose
}

check() {
	dune runtest --build-dir=.testenv --no-buffer --verbose
}

package() {
	dune install \
		--destdir="$pkgdir" \
		--docdir=/.omit \
		merlin merlin-lib dot-merlin-reader

	install -Dvm644 LICENSE \
		-t "$pkgdir"/usr/share/licenses/$pkgname/
}

emacs() {
	pkgdesc="$pkgdesc in Emacs"
	depends="$pkgname=$pkgver-r$pkgrel"
	install_if="$pkgname=$pkgver-r$pkgrel emacs"

	amove usr/share/emacs
}

vim() {
	pkgdesc="$pkgdesc in vim"
	depends="$pkgname=$pkgver-r$pkgrel python3"
	install_if="$pkgname=$pkgver-r$pkgrel vim"

	amove usr/share/merlin/vim
}

dev() {
	default_dev

	cd "$pkgdir"

	local path; find usr/lib/ocaml \( \
			-name '*.cmt' -o \
			-name '*.cmti' -o \
			-name '*.ml' -o \
			-name '*.mli' \
		\) | while read -r path
	do
		amove "$path"
	done
}

prepare_py_provides() {
	local datadir="${subpkgdir:-$pkgdir}"
	local pkgbasedir=${pkgbasedir:-"$startdir/pkg"}
	local controldir="$pkgbasedir"/.control.${subpkgname:-$pkgname}

	options_has "!tracedeps" && return 0

	ocaml4-abuild-find provides \
		"$datadir"/usr/lib/ocaml \
		"$controldir" \
		"$pkgver-r$pkgrel"
}

scan_python3_dependency() {
	local controldir="$2" datadir="$3"

	ocaml4-abuild-find requires \
		"$datadir"/usr/lib/ocaml \
		"$controldir"
}

sha512sums="
ebefec44c7f54b10aa85895baacecf778930ee0c4cd5a257836755ccadd9018c20379511e254efa98ed7d48fff9ba9eeb93b45a722cc1d2d61f51570cc57985d  merlin-4.14-414.tar.bz2
"
